<template>
	<view class="main-item-container">
		<image :src="data.fruitInfo.pic_url" mode="aspectFit" class="product_img"></image>
		<view class="prodcut_info">
			<view class="name">{{data.fruitInfo.title}}</view>
			<view class="intro">{{data.fruitInfo.info}}</view>
			<view class="sell_count">
				月售 {{data.fruitInfo.sell_count}}
			</view>
			<view class="price">
				<text>￥</text>{{data.fruitInfo.discount_price}}
				<text class="discount" v-show="data.fruitInfo.discount_price!=data.fruitInfo.price">{{(data.fruitInfo.discount_price/data.fruitInfo.price).toFixed(2)}}折</text>
			</view>
			<image @tap="changeCount(1)" v-show="data.count==0" class="add-mi" src="@/page_fruit/static/fruit/add.png" mode="widthFix"></image>
		</view>
		<view class="btnContainer">
			<view @tap="goEdit">编辑</view>
			<view @tap="del">删除</view>
		</view>
	</view>
	
</template>

<script>
	import {reactive, onMounted,watch} from 'vue'
	import global from '../../config/global.js'
	export default{
		name:"fruit-admin-show-item",
		setup(props,context){
			let data = reactive({
				fruitInfo:{},
			})
			
			onMounted(()=>{
				// console.log(props.fruitInfo)
				data.fruitInfo = props.fruitInfo
			})
			
			watch(props,(newProps) => {
				console.log(newProps)
				data.fruitInfo = newProps.fruitInfo
			})
			
			function del(){
				uni.showModal({
					title:"确认删除吗？",
					success: (res) => {
						if(res.confirm){
							uni.request({
								url:`${global.BASE_URL}/fruit/deleteFruitItem/${data.fruitInfo.id}`,
								method:"DELETE",
								header:{
									"mytoken":uni.getStorageSync("localtoken")
								},
								success: (res) => {
									console.log(res)
									if(res.data.code){
										context.emit('refresh',1)
									}
								},
								fail: (err) => {
									console.log(err)
								}
							})
						}
					}
				})
			}
			
			function goEdit(){
				uni.navigateTo({
					url:"/page_fruit/fruit/fruit-admin-edit-item?fruitItem="+encodeURIComponent(JSON.stringify(data.fruitInfo))
				})
			}
			
			return {
				data,
				del,
				goEdit
			}
		}
	}
</script>

<style scoped lang="scss">
	.main-item-container{
		width: 100%;
		position: relative;
		display: flex;
		align-items: center;
		padding: 5rpx 0;
		
		& .btnContainer{
			position: absolute;
			right: 10rpx;
			
			view {
				padding: 10rpx;
				text-decoration: underline
			}
			
		}
		
		& .product_img{
			width: 180rpx;
			height: 180rpx;
			
		}
			
		& .add-mi{
			position: absolute;
			width: 45rpx;
			right: 15rpx;
			bottom: 10rpx;
		}
		
		& .add-some{
			position: absolute;
			display: flex;
			justify-content: space-between;
			width: 170rpx;
			right: 15rpx;
			bottom: 10rpx;
			
			& image{
				width: 40rpx;
			}
		}
			
		& .prodcut_info{
			height: 160rpx;
			margin-top: 10rpx;
			margin-left: 15rpx;
			width: 370rpx;
			// background-color: aqua;
				
			& .name{
				// position: sticky;
				// top: 40rpx;
				font-size: 32rpx;
				text-overflow: ellipsis; 
				overflow: hidden; 
				word-break: break-all; 
				white-space: nowrap;
				height: 40rpx;
				line-height: 40rpx;
			}
			
			& .intro,.sell_count{
				font-size: 28rpx;
				color: #bbbbbb;
				text-overflow: ellipsis;
				overflow: hidden; 
				word-break: break-all; 
				white-space: nowrap;
			}
			
			& .price{
				font-size: 35rpx;
				height: 50rpx;
				line-height: 50rpx;
				
				& text{
					font-size: 22rpx;
				}
				
				& .discount{
					padding: 5rpx 10rpx;
					height: 35rpx;
					line-height: 35rpx;
					background-color: #DE6868;
					color: white;
					border-radius: 10rpx;
				}
			}
		}
	}
			
</style>